package cn.tedu.myweb.myweb.Controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.tedu.myweb.myweb.util.HttpClient;
import cn.tedu.myweb.myweb.util.Url;

@RestController
public class MainController {
	@Value("${myweb.myurl}")
	private String Myurl;
	@Value("${myweb.tedurl}")
	private String Tedurl;
	/*@GetMapping("list")
	public MyList getList(HttpServletRequest request){
		List<String> list = new ArrayList<>();
		MyList mylist = new MyList();
		list.add("1");
		list.add("2");
		list.add("1");
		String path = request.getServletContext().getRealPath("list");
		File file = new File(path);
		if(!file.exists()) {
			file.mkdir();
		}
		
		mylist.setList(list);
		return mylist;
	}*/
	
	@RequestMapping("getVideo")
	public Url getUrl(@RequestParam("day")String fday,String time,HttpServletRequest request) throws IOException {
		String[] str = fday.split("\\D");
		Url url = new Url();
		if(str.length!=3) {
			url.setState(20);
			url.setMsg("日期输入错误!");
			return url;
		}
		//String year = str[0];
		String month = str[1];
		String day = str[2];
		if(Integer.valueOf(month)<10) {
			month="0"+month;
		}
		if(Integer.valueOf(day)<10) {
			day="0"+day;
		}
		String name = "jsd1902"+month+day+time;
		String end = name+"/"+name+".m3u8";
		name = Myurl+end;
		HttpClient.getData(end, Tedurl);
		url.setState(10);
		request.getSession().setAttribute("url", name);
		return url;
	}
	@RequestMapping("getVideoUrl")
	public Url getVideoUrl(HttpServletRequest request) {
		HttpSession session=request.getSession();
		Object obj = session.getAttribute("url");
		Url url = new Url();
		if(session==null || obj==null){
			url.setState(23);
			url.setMsg("获取超时,请返回主页!");
			return url;
		}
		String uri = obj.toString();
		
		url.setState(10);
		url.setUrl(uri);
		return url;
	}
}
